//
//  VisitorView.swift
//  weibo
//
//  Created by nick on 15/9/23.
//  Copyright © 2015年 cn.nick. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUp()
    }
    
    func setUpInfo(image: String?, title: String) {
        // 不是首页
        if image != nil {
            
            iconView.image = UIImage(named: image!)
            textLabel.text = title
            houseView.hidden = true
//            bringSubviewToFront(iconView)
            sendSubviewToBack(coverView)
        } else {
            setAnima()
        }
        
    }
    
    func setAnima() {
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        anima.toValue = 2 * M_PI
        anima.repeatCount = MAXFLOAT
        anima.duration = 20
        
        anima.removedOnCompletion = false
        
        iconView.layer.addAnimation(anima, forKey: nil)
    }
    
    func setUp() {
    
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        bringSubviewToFront(houseView)
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
//        houseView.translatesAutoresizingMaskIntoConstraints = false
        // 转轮
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.centerY.equalTo(snp_centerY)
        }
        // 房子
        houseView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        // 标题
        textLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom)
            make.width.equalTo(230)
            make.height.equalTo(35)
        }
        // 注册
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(textLabel.snp_left)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        // 登录
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(textLabel.snp_right)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        // 遮盖
        /**
        VFL:     可视化格式语言
        H:      水平方向
        V:      垂直方向
        |       边界
        []      包含控件的名称字符串，对应关系在 views 字典中定义
        ()      定义控件的宽/高
        metrics: 是一个字典 [String: NSNumber] - 定义 VFL 中的约束数值
        views:   是一个字典 [String: 子视图] - 定义 VFL 中的子视图的名称，能够保证准确设置控件对象
        */
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cover]-0-|", options: [], metrics: nil, views: ["cover": coverView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cover]-(height)-[registBtn]", options: [], metrics: ["height": -70], views: ["cover": coverView, "registBtn": registerBtn]))
        coverView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        }
    
    
    // 懒加载控件
    private lazy var houseView: UIImageView = {
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    }()
    private lazy var iconView: UIImageView = {
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    }()
    private lazy var coverView: UIImageView = {
        return UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    }()
    private lazy var textLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    lazy var registerBtn: UIButton = UIButton(title: "注册", imageName: "common_button_white_disable", titleColor:UIColor.orangeColor())
    lazy var loginBtn: UIButton = UIButton(title: "登录", imageName: "common_button_white_disable", titleColor:UIColor.darkGrayColor())
    
    
    
}
